package com.xl.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xl.action.Action;
import com.xl.bean.ActionMapping;
import com.xl.bean.ResultView;
import com.xl.mananger.ActionManager;
import com.xl.mananger.MappingManager;


public class MVCController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2391385399478278867L;

	/**
	 * Constructor of the object.
	 */
	public MVCController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
		 doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取servlet的请求路径 例如  loginAndRegister(包名称)/login(action的name和实体类的name).do
		String [] paths = MappingManager.getPaht(request);
		//获取包名
        String packageName = paths[0];
        //获取action名称
        String actionName = paths[1];
        //根据action名称获取对象的action配置文件封装对象
        ActionMapping actionMapping = MappingManager.getActionValue(packageName, actionName);
        //根据类名反射Action对象
        Action action = ActionManager.crate(actionMapping.getActionClass());
        //执行action 的execute方法
        String msg = action.execute(request,response);
        //根据返回的信息获取相应的视图封装对象
        ResultView view = actionMapping.getResultMap().get(msg);
        //获取视图的路径
        String url = view.getResultValue();
        //跳转
        if(view.isRedirect())
        {
        	
        	response.sendRedirect(request.getContextPath()+url);
        	
        }else
        {
        	request.getRequestDispatcher(url).forward(request, response);
        	
        }
        
	}

	/**
	 * Initialization of the servlet. <br>
	 * 初始化时将struts的配置文件的信息保存在相应的map中
	 * @throws ServletException if an error occurs`
	 */
	public void init() throws ServletException {
	   String [] config = this.getInitParameter("config").split(",");
	   MappingManager.inIt(config);
	}

}
